/**
 * @author monologu
 */

/*$(document).ready(function(){
	$('#userEmail').autoMail({
		emails:['qq.com','163.com','126.com','sina.com','sohu.com','yahoo.cn','gmail.com','hotmail.com','live.cn']
	});
});*/

var emailValidateCode;
var flag = {
		username : false,
		nickname : false,
		password : false,
		confirmPassword : false,
		email : false,
		emailValidate : false,
		agreePact : true,
		userWebSite : false
};

/**
 * 
 * @param id
 * @returns dom Object
 */
function domId(id){
	return document.getElementById(id);
}

/**
 * 
 * @param name
 * @returns dom Object
 */
function domName(name){
	return document.getElementByName(name);
}

function getRootPath(){
	var projectPath = window.document.location.href;//浏览器地址
	var filterPath = window.document.location.pathname;//项目路径后面的url
	var filterSize = projectPath.indexOf(filterPath);
	var projectName = filterPath.substring(0,filterPath.substr(1).indexOf('/')+1);
	return (projectPath.substring(0 ,  filterSize) + projectName + "/");
}

/**
 * 
 * @param item
 * @param expression
 * @returns
 */
function valid(item , expression) {
	var patten = new RegExp(expression);
	return patten.test(item);
}

$(function() {
	$("#userUsername").blur(function() {
		var userUsername = $.trim($('#userUsername').val());
		if (valid(userUsername , /^\w+$/) && userUsername.length > 2 && userUsername.length < 12) {
			flag.username = true;
			$("#userUsernameMessage").html("");
		} else {
			flag.username = false;
			$("#userUsernameMessage").html("用户名非法(不能为空且长度为3-12)");
		}
	});
});

$(function() {
	$("#userNickname").blur(function() {
		var userNickname = $.trim($('#userNickname').val());
		if (valid(userNickname , /^[0-9a-z\\u4e00-\\u9fa5]+$/) && userNickname.length > 0 && userNickname.length < 12) {
			flag.nickname = true;
			$("#userNicknameMessage").html("");
		} else {
			flag.nickname = false;
			$("#userNicknameMessage").html("昵称不能包含特殊字符");
		}
	});
});

$(function() {
	$("#userPassword").blur(function() {
		var userPassword = $.trim($('#userPassword').val());
		if (valid(userPassword , /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,16}$/)) {
			flag.password = true;
			$("#userPasswordMessage").html("");
		} else {
			flag.password = false;
			$("#userPasswordMessage").html("密码不符合要求");
		}
	});
});

$(function() {
	$("#confirmPassword").blur(function() {
		var confirmPassword = $.trim($('#userPassword').val());
		var userPassword = $.trim($('#userPassword').val());
		if (valid(confirmPassword , /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,16}$/) && confirmPassword == userPassword) {
			flag.confirmPassword = true;
			$("#confirmPasswordMessage").html("");
		} else {
			flag.confirmPassword = false;
			$("#confirmPasswordMessage").html("两次输入的密码不一样");
		}
	});
});

$(function() {
	$("#userEmail").blur(function() {
		var userEmail = $.trim($('#userEmail').val());
		if (valid(userEmail , /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)) {
			flag.email = true;
			$("#userEmailMessage").html("");
		} else {
			flag.email = false;
			$("#userEmailMessage").html("非法邮箱");
		}
	});
});

$(function() {
	$("#emailValidate").blur(function() {
		var emailValidate = hex_md5($('#emailValidate').val());
		if (emailValidate == emailValidateCode) {
			flag.emailValidate = true;
			$("#emailValidateMessage").html("");
		} else {
			flag.emailValidate = false;
			$("#emailValidateMessage").html("验证码输入不正确");
		}
	});
});

$(function(){
	$('#sendEmail').click(function(){
		$.ajax({
			type:"post", 
			url:"sendEmailCode.action?email=" + ($.trim($('#userEmail').val())),
			dataType: "json", 
			success: function (data) {
				emailValidateCode = data.sendEmailCode;
				flag.email = true;
				$("#userEmailMessage").html("");
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				flag.email = false;
				$("#userEmailMessage").html("出错啦!!!");
			}
		});
	});
});

$(function(){
	$("#submitFormPage").click(function(){
		if(flag.username == true && flag.nickname == true
				&& flag.password == true && flag.confirmPassword == true
				&& flag.email == true && flag.emailValidate == true 
				&& flag.agreePact == true){
			var formObj = domId("pageForm");
			formObj.action = getRootPath() + "doRegister.action";
			formObj.submit();
		} else {
			alert("请确认表单是否填写完整");
		}
	});
});
